<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/21 0021
 * Time: 15:19
 */
namespace Api\Controller;


class StoreController extends BaseController
{
    /**
     * 获取门店列表
     */
    public function index()
    {
        $title = I('param.title','');
        $where = [];
        if(!empty($title)) {
            $where['title'] = array('like',"%{$title}%");
        }
        $where['status'] = array('eq','1');
        $region = I('post.region');
        $category_name = I('post.category_id');
        if(!empty($category_name)) {
            if($category = M('category')->where(array('title'=>$category_name))->find()) {
                if(!empty($category)) {
                    $where['category'] = array('like',"%{$category['id']}%");
                }

            }
        }
        if(!empty($region)) {
            $where['area'] = array('like',"%{$region}%");
        }
        //var_dump($where);die;
        $lat = I('param.lat');
        $lng = I('param.lng');
        empty($lat) && $this->error('lat缺失！');
        empty($lng) && $this->error('lng缺失！');
        $list = M('ShopStore')->where($where)->field("id,sid,images,title,describe,thumb,(
    6371 * acos (
      cos ( radians({$lat}) )
      * cos( radians( lat ) )
      * cos( radians( lng ) - radians({$lng}) )
      + sin ( radians({$lat}) )
      * sin( radians( lat ) )
    )
  ) AS distance")->order('distance asc')->limit(100)->select();

        foreach ($list as &$item) {

            $item['thumb'] = thumb_path($item['thumb']);
            //$title_img = array_filter(explode(',',$item['images']));
            $item['img'] =array_map(function($v){
                return thumb_path($v);
            },array_filter(explode(',',$item['images'])));
            $up_num = M('ShopStoreScreen')->where(['sid'=>$item['id']])->sum('up_num');
            $in_num = M('ShopStoreScreen')->where(['sid'=>$item['id']])->sum('in_num');
            $item['left_count']=0;
            $item['distance'] = floor($item['distance']);
            $item['screen'] = M('ShopStoreScreen')->where(['sid'=>$item['id']])->field('id,title,equipment,up_num,in_num')->select();
            foreach ($item['screen'] as $k=>$v){

                $item['screen'][$k]['use_num'] = M('AdOrderList')->where("(`end_time` >".time()." )and (`screen_id` =". $v['id'].")")->count();
                $item['screen'][$k]['up_num'] = $item['screen'][$k]['up_num'] - M('AdOrderList')->where("(`end_time` >".time()." )and (`screen_id` =". $v['id']." and `ad_type` = 1)")->count();
                $item['left_count']+= $item['screen'][$k]['up_num'];
                $item['screen'][$k]['in_num'] =$item['screen'][$k]['in_num'] - M('AdOrderList')->where("(`end_time` >".time()." )and (`screen_id` =". $v['id']." and `ad_type` = 2)")->count();
                $item['left_count']+= $item['screen'][$k]['in_num'];
            }
        }
        $this->success('获取门店列表成功！',$list);
    }

    /**
     * 获取门店详情
     * @param $id
     */
    public function read($id)
    {
        $where = [
            'id' => array('eq',$id),
            'status' => array('eq','1')
        ];
        $store = M('ShopStore')->where($where)->field('id,sid,category_id,title,describe,thumb,images,tag,area,lat,lng,address')->find();
        empty($store) && $this->error('门店不存在或已被禁用！');

        $store['thumb_img'] = thumb_path($store['thumb']);
        $store['thumb_id'] = $store['thumb'];
        $store['images_id'] = explode(',',$store['images']);
        $store['phone'] = M('shop')->where(['id'=>$store['sid']])->getField('phone');
        $store['images_img'] = array_map(function($v){
            return thumb_path($v);
        },array_filter(explode(',',$store['images'])));
        $store['tag'] = array_filter(explode('|',$store['tag']));
        $store['category_id'] = explode(',',$store['category_id']);
        $this->success('获取门店信息成功！',$store);
    }

    /**
     * 添加门店
     */
    public function add_store()
    {
        $id = I('id');
        $data['sid'] = I('sid'); //商户名称
        $data['title'] = I('title');//店铺名称
        $data['describe'] = I('describe');//店铺描述
        $data['thumb'] = I('thumb'); //缩略图
        $data['images'] = I('images'); //店铺图集
        $data['tag'] = I('tag'); //标签
        $data['area'] = I('area'); //地区
        $data['address'] = I('address'); //需求详细地址
        $data['category_id'] = I('category_id');//所属行业id用,分隔
        $data['lat'] = I('lat');//纬度
        $data['lng'] = I('lng');//经度
        $data['status'] = 0;
        if($id==''||empty($id)){
            $re = M('ShopStore')->add($data);
        }else{
            $re = M('ShopStore')->where(['id'=>$id])->save($data);
        }
        if($re){
            $this->success('已提交申请，请等待审核');
        }else{
            $this->error('申请提交失败');
        }
    }

    /**
     * 获取当前商户下所有店铺
     */
    public function get_store_list()
    {
        $user_id = I('user_id');
        if($user_id==''||empty($user_id)){
            $this->error('参数有误');
        }
        $sid = M('shop')->where(['user_id'=>$user_id])->getField('id');
        $data['list'] = M('ShopStore')->where(['sid'=>$sid,'status'=>1])->select();
        $data['sid'] = $sid;
        $this->success('获取成功',$data);
    }

    // 获取某个标签的配置参数
    public function group() {
        $id     =   I('get.id',1);
        $list   =   M("Config")->where(array('status'=>1,'group'=>$id))->field('id,name,title,extra,value,remark,type')->order('sort')->select();
       $this->success('配置信息拉取成功',$list);
    }
    public function get_category(){
        $id = I('id');
        $where['status'] = array('eq','1');
        $region = I('region');
        if(!empty($region)) {
            $where['area'] = array('like',"%{$region}%");
        }
        $where['category_id'] = array('like',"%{$id}%");
        $list = M('ShopStore')->where($where)->select();
        foreach ($list as  &$v){
            $v['thumb'] = thumb_path($v['thumb']);
            //$title_img = array_filter(explode(',',$item['images']));
            $v['img'] =array_map(function($v){
                return thumb_path($v);
            },array_filter(explode(',',$v['images'])));
            $v['screen'] = M('shop_store_screen')->where(['sid'=>$v['id']])->select();
            foreach ($v['screen'] as $k=>$vs){

                $v['screen'][$k]['use_num'] = M('AdOrderList')->where("(`end_time` >".time()." )and (`screen_id` =". $vs['id'].")")->count();
                $v['screen'][$k]['up_num'] = $v['screen'][$k]['up_num'] - M('AdOrderList')->where("(`end_time` >".time()." )and (`screen_id` =". $vs['id']." and `ad_type` = 1)")->count();
                $v['left_count']+= $v['screen'][$k]['up_num'];
                $v['screen'][$k]['in_num'] =$v['screen'][$k]['in_num'] - M('AdOrderList')->where("(`end_time` >".time()." )and (`screen_id` =". $vs['id']." and `ad_type` = 2)")->count();
                $v['left_count']+= $v['screen'][$k]['in_num'];
            }
        }
        $this->success('门店获取成功',$list);
    }
}